package com.example.cp.service;

import com.alibaba.fastjson.JSONArray;
import com.example.cp.config.HiddenProperties;
import com.example.cp.pojo.DictData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.Arrays;
import java.util.List;

/**
 * html调用 thymeleaf 实现字典读取
 * 
 * @author hemin
 */
@Service("dict")
public class DictService {

    @Autowired
    HiddenProperties hiddenProperties;

    @Autowired
    RestTemplate restTemplate;


    /**
     * 根据字典类型查询字典数据信息
     * 
     * @param dictType 字典类型
     * @return 参数键值
     */
    public List<DictData> getTypeByDict(String dictType) {
        String getTypeUrl = hiddenProperties.getHeaderUrl() + "/dict/dict?dictName="+dictType;
        DictData[] forObject = restTemplate.getForObject(getTypeUrl, DictData[].class);
        return Arrays.asList(forObject);
    }

    /**
     * 根据字典类型查询字典数据信息
     *
     * @param dictType 字典类型
     * @return 参数键值
     */
    public String getTypeByJson(String dictType) {

        String getTypeUrl = hiddenProperties.getHeaderUrl() + "/dict/dictSelect?dictName="+dictType;
        JSONArray jsonArray = restTemplate.getForObject(getTypeUrl, JSONArray.class);
        String jsonString = jsonArray.toJSONString();
        return jsonString;
    }

}
